package retrofitdemo.com.night.activity;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.TextureMapView;
import com.amap.api.maps.model.CrossOverlay;
import com.amap.api.maps.model.CrossOverlayOptions;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.model.AMapLaneInfo;
import com.amap.api.navi.model.AMapModelCross;
import com.amap.api.navi.model.AMapNaviCameraInfo;
import com.amap.api.navi.model.AMapNaviCross;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.AMapTrafficStatus;
import com.amap.api.navi.model.NaviInfo;
import com.amap.api.navi.view.NextTurnTipView;
import com.amap.api.navi.view.TrafficProgressBar;
import com.amap.api.navi.view.ZoomInIntersectionView;
import com.autonavi.ae.gmap.gloverlay.AVectorCrossAttr;

import java.io.InputStream;
import java.util.List;

import retrofitdemo.com.night.R;
import retrofitdemo.com.night.utils.PermissionUtil;
import retrofitdemo.com.night.utils.listener.AMapCameraOverlay;
import retrofitdemo.com.night.utils.listener.CarOverlay;
import retrofitdemo.com.night.utils.listener.DensityUtils;
import retrofitdemo.com.night.utils.listener.DriveWayLinear;
import retrofitdemo.com.night.utils.listener.NaviUtil;

import static retrofitdemo.com.night.bean.ApiService.LOGINPRESSION;
import static retrofitdemo.com.night.bean.ApiService.REQEEST_CDOE_GPS;

public class AllCustomNaviActivity extends BaseCustomActivity {

    private TrafficProgressBar mTrafficBarView;
    private DriveWayLinear mDriveWayView;
    private ZoomInIntersectionView mZoomInIntersectionView;
    private AMapCameraOverlay cameraOverlay;
    private TextView textNextRoadDistance;
    private TextView textNextRoadName;
    private NextTurnTipView nextTurnTipView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_custom_navi_view);
        mAMapNaviView = (TextureMapView) findViewById(R.id.navi_view);
        mAMapNaviView.onCreate(savedInstanceState);
        mTrafficBarView = (TrafficProgressBar) findViewById(R.id.myTrafficBar);
        mDriveWayView = (DriveWayLinear) findViewById(R.id.myDriveWayView);
        mZoomInIntersectionView = (ZoomInIntersectionView) findViewById(R.id.myZoomInIntersectionView);
        textNextRoadDistance = (TextView) findViewById(R.id.text_next_road_distance);
        textNextRoadName = (TextView) findViewById(R.id.text_next_road_name);
        nextTurnTipView = (NextTurnTipView) findViewById(R.id.icon_next_turn_tip);

        initPermission();
        cameraOverlay = new AMapCameraOverlay(this);
        mAMapNaviView.getMap().getUiSettings().setZoomControlsEnabled(false);
        mAMapNaviView.getMap().setOnMapLoadedListener(new AMap.OnMapLoadedListener() {
            @Override
            public void onMapLoaded() {
                mAMapNaviView.getMap().setPointToCenter(mAMapNaviView.getWidth() / 2, mAMapNaviView.getHeight() / 2);
                mAMapNaviView.getMap().moveCamera(CameraUpdateFactory.zoomTo(16f));
                carOverlay = new CarOverlay(AllCustomNaviActivity.this, mAMapNaviView);
            }
        });
    }

    private void initPermission() {
        if (PermissionUtil.loginPermission(this, LOGINPRESSION)) {
            if (!PermissionUtil.isOPen(AllCustomNaviActivity.this)) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivityForResult(intent, REQEEST_CDOE_GPS);
            } else {
                initLocation();
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case LOGINPRESSION:
                initGPS();
                break;
            case REQEEST_CDOE_GPS:
                initLocation();
                break;
            default:
        }
    }

    public void initGPS() {
        if (!PermissionUtil.isOPen(this)) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivityForResult(intent, REQEEST_CDOE_GPS);
        } else {
            initLocation();
        }
    }

    //初始化地图
    private void initLocation() {
        mLocationClient = new AMapLocationClient(getApplicationContext());
        mLocationClient.setLocationListener(listener);
        mLocationClient.startLocation();
    }

    /**
     * ------- 导航基本信息的回调 -----
     */
    @Override
    public void onNaviInfoUpdate(NaviInfo naviInfo) {
        super.onNaviInfoUpdate(naviInfo);
        int allLength = mAMapNavi.getNaviPath().getAllLength();

        /**
         * 导航路况条 更新
         */
        List<AMapTrafficStatus> trafficStatuses = mAMapNavi.getTrafficStatuses(0, 0);
        mTrafficBarView.update(allLength, naviInfo.getPathRetainDistance(), trafficStatuses);

        /**
         * 更新路口转向图标
         */
        nextTurnTipView.setIconType(naviInfo.getIconType());

        /**
         * 更新下一路口 路名及 距离
         */
        textNextRoadName.setText(naviInfo.getNextRoadName());
        textNextRoadDistance.setText(NaviUtil.formatKM(naviInfo.getCurStepRetainDistance()));

        /**
         * 绘制转弯的箭头
         */
        drawArrow(naviInfo);
    }

    /**
     * GPS 定位信息的回调函数
     *
     * @param location 当前定位的GPS信息
     */
    @Override
    public void onLocationChange(AMapNaviLocation location) {
        if (carOverlay != null && location != null) {
            carOverlay.draw(mAMapNaviView.getMap(), new LatLng(location.getCoord().getLatitude(), location.getCoord().getLongitude()), location.getBearing());
        }
    }

    /**
     * ----- start 车道信息的回调 start -------
     */
    @Override
    public void showLaneInfo(AMapLaneInfo aMapLaneInfo) {
        mDriveWayView.setVisibility(View.VISIBLE);
        mDriveWayView.buildDriveWay(aMapLaneInfo);
    }

    @Override
    public void hideLaneInfo() {
        mDriveWayView.hide();
    }
    /** ----- ebd 车道信息的回调 end -------*/

    /**
     * ----- start 路口放大图 start -------
     */
    @Override
    public void showCross(AMapNaviCross aMapNaviCross) {
        mZoomInIntersectionView.setIntersectionBitMap(aMapNaviCross);
        mZoomInIntersectionView.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideCross() {
        mZoomInIntersectionView.setVisibility(View.GONE);
    }

    private CrossOverlay crossOverlay;

    @Override
    public void showModeCross(AMapModelCross aMapModelCross) {
        try {
            AVectorCrossAttr attr = new AVectorCrossAttr();
            // 设置显示区域
            attr.stAreaRect = new Rect(0, DensityUtils.dp2px(getApplicationContext(), 50),
                    DensityUtils.getScreenWidth(getApplicationContext()), DensityUtils.dp2px(getApplicationContext(), 300));
            //        attr.stAreaRect = new Rect(0, dp2px(48), nWidth, dp2px(290));
            attr.stAreaColor = Color.argb(217, 95, 95, 95);/* 背景颜色 */
            attr.fArrowBorderWidth = DensityUtils.dp2px(getApplicationContext(), 22);/* 箭头边线宽度 */
            attr.stArrowBorderColor = Color.argb(0, 0, 50, 20);/* 箭头边线颜色 */
            attr.fArrowLineWidth = DensityUtils.dp2px(getApplicationContext(), 18);/* 箭头内部宽度 */
            attr.stArrowLineColor = Color.argb(255, 255, 253, 65);/* 箭头内部颜色 */
            attr.dayMode = false;
            attr.fArrowLineWidth = 18;/* 箭头内部宽度 */
            attr.stArrowLineColor = Color.argb(255, 255, 253, 65);/* 箭头内部颜色 */
            attr.dayMode = true;
            InputStream inputStream = getResources().getAssets().open("vector3d_arrow_in.png");
            crossOverlay = mAMapNaviView.getMap().addCrossOverlay(new CrossOverlayOptions().setAttribute(attr).setRes(BitmapFactory.decodeStream(inputStream)));
            crossOverlay.setData(aMapModelCross.getPicBuf1());
            crossOverlay.setVisible(true);
            inputStream.close();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Override
    public void hideModeCross() {
        if (crossOverlay != null) {
            crossOverlay.setVisible(false);
        }
    }

    /** ----- end 路口放大图 end ------- */

    /**
     * ----- start 电子眼  start --------
     */
    @Override
    public void updateCameraInfo(AMapNaviCameraInfo[] aMapCameraInfos) {

        if (cameraOverlay != null) {
            cameraOverlay.draw(mAMapNaviView.getMap(), aMapCameraInfos);
        }

    }

    /**
     * ----- end 电子眼  end --------
     */

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mZoomInIntersectionView != null) {
            mZoomInIntersectionView.recycleResource();
        }

        if (crossOverlay != null) {
            crossOverlay.remove();
        }

        if (cameraOverlay != null) {
            cameraOverlay.destroy();
        }
    }
}
